package com.rtx.edi.platform.jobextend.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.jobextend.bean.dto.PlJobDTO;
import com.rtx.edi.platform.jobextend.bean.po.PlJob;
import com.rtx.edi.platform.jobextend.service.PlJobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = {"调度任务"})
@RequestMapping("/v1/edi/pljob")
@RestController
public class PlJobController {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	PlJobService plJobService;

	@ApiOperation(value = "查询调度任务_分页", notes = "查询调度任务_分页")
	@PostMapping("selectjobpage")
	public EdiResponse selectPlJobList(@RequestBody(required = false) PlJobDTO dto){
		PlJob po = new PlJob();
		BeanUtils.copyProperties(dto, po);
		
		Page<PlJob> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		plJobService.selectPlJobList(pageParam, po);
		return EdiResponse.success().data("page",pageParam);
	}
	
	@ApiOperation(value = "新增调度任务", notes = "新增调度任务")
	@PostMapping("addJob")
	public EdiResponse add(@RequestBody PlJobDTO dto){
		try {
			plJobService.add(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}，添加异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	
	@ApiOperation(value = "启用调度任务", notes = "启用调度任务")
	@PostMapping("activeJob")
	public EdiResponse active(@RequestBody PlJobDTO dto){
		try {
			plJobService.activeJOb(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}-启用异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	@ApiOperation(value = "停用调度任务", notes = "停用调度任务")
	@PostMapping("disactiveJob")
	public EdiResponse disactive(@RequestBody PlJobDTO dto){
		try {
			plJobService.disactive(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}-停用异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	@ApiOperation(value = "修改调度任务触发时间", notes = "修改调度任务触发时间")
	@PostMapping("updJobCron")
	public EdiResponse updJob(@RequestBody PlJobDTO dto){
		try {
			plJobService.updJobCron(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}-修改异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	
	@ApiOperation(value = "删除调度任务", notes = "删除调度任务")
	@PostMapping("delJob")
	public EdiResponse delJob(@RequestBody PlJobDTO dto){
		try {
			plJobService.delJob(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}-删除异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	@ApiOperation(value = "触发调度任务执行", notes = "触发调度任务执行")
	@PostMapping("executeJob")
	public EdiResponse executeJob(@RequestBody PlJobDTO dto){
		try {
			plJobService.execute(dto);
			return EdiResponse.success();
		} catch (Exception exception) {
			logger.error("定时任务名称：{}，定时任务描述：{}-删除异常-",dto.getJobName(),dto.getDescription(),exception);
			return EdiResponse.failed().returnMgr(exception.getMessage());
		}
	}
	
	
	@ApiOperation(value = "修改定时任务", notes = "修改定时任务")
	@PostMapping("updjobBase")
	public EdiResponse updjobBase(@RequestBody PlJobDTO dto) throws Exception {
		plJobService.updJobBase(dto);
		return EdiResponse.success();
	}
}
